package com.base.home.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.alibaba.android.arouter.exception.HandlerException;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.ToastUtil;
import com.base.home.R;
import com.base.home.bean.ConcertSelectEntity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sensyang.
 * on 2018/11/16 0016.
 * Email:532245792@qq.com
 * desc:
 */

public class BannerAdapter extends RecyclerView.Adapter<BannerAdapter.ViewHolder> {

    private Context mContext;
    List<ConcertSelectEntity.BannersBean> list = new ArrayList<>();

    public BannerAdapter(Context c, List<ConcertSelectEntity.BannersBean> list) {
        mContext = c;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_banner, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (mContext != null) {

            RequestOptions options = new RequestOptions()
                    .error(R.drawable.ic_empty)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.ic_empty);
            holder.img.getLayoutParams().width = getScreenWidth(mContext) - 160;
            Glide.with(mContext)
                    .load(list.get(position).getLoadPic())
                    .apply(options)
                    .into(holder.img);
            holder.mCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (list.get(position).getWebUrl().startsWith("http")){
                        ARouter.getInstance().build("/sweb/common")
                                .withInt("key",4)
                                .withString("url",list.get(position).getWebUrl())
                                .withString("title","江苏中越科技")
                                .withString("name","测试")
                                .navigation();
                    }else {
                        try {
                            ARouter.getInstance().build(list.get(position).getMClass()).withString("id",list.get(position).getId()).navigation();
                        }catch (HandlerException e){
                            ToastUtil.show("暂无权限");
                        }
                    }
                }
            });
        }
    }

    /**
     * 获得屏幕宽度
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics( outMetrics);
        return outMetrics .widthPixels ;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        LinearLayout mCardView;
        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            mCardView = itemView.findViewById(R.id.card);
        }
    }
}

